﻿using System;
using RanchBuddy.Core.DataAccess;
using RanchBuddy.Core.Domain;
using RanchBuddy.Core.Domain.Impl;
using StructureMap;

namespace RanchBuddy.Core.Services.Impl
{
    [Pluggable("Default")]
    public class AccountService : IAccountService
    {
        public IAccount GetAccountByUsername(string Username)
        {
            IAccount a = ObjectFactory.GetInstance<IAccountRepository>().GetAccountByUsername(Username);
            return a;
        }

        public IAccount GetAccountByEmail(string Email)
        {
            IAccount a = ObjectFactory.GetInstance<IAccountRepository>().GetAccountByEmail(Email);
            return a;
        }

        public string GetOpenPasswordByUsername(string Username)
        {
            string result = "";
            IAccount a = ObjectFactory.GetInstance<IAccountRepository>().GetAccountByUsername(Username);
            if (a != null)
                result = Cryptography.Decrypt(a.Password, "tm-es@as");
            return result;
        }

        public string GetOpenPasswordByEmail(string Email)
        {
            string result = "";
            IAccount a = ObjectFactory.GetInstance<IAccountRepository>().GetAccountByEmail(Email);
            if (a != null)
                result = Cryptography.Decrypt(a.Password, "tm-es@as");
            return result;
        }

        public IStatus SaveAccount(IAccount account)
        {
            IStatus s = ObjectFactory.GetInstance<IStatus>();
            IRepositoryStatus rs = ObjectFactory.GetInstance<IAccountRepository>().SaveAccount(account);
            s.StatusType = rs.StatusType;
            s.AddMessages(rs.GetMessages());
            s.AddRuleViolations(rs.GetRuleViolations());
            s.SetObject(rs.GetObject<IAccount>());
            return s;
        }

        public IStatus ChangePassword(IAccount account, string OldPassword, string NewPassword, string ConfirmPassword)
        {
            IStatus status = ObjectFactory.GetInstance<IStatus>();

            if(NewPassword != ConfirmPassword)
            {
                status.AddMessage(HelpMessageService.GetHelpMessageBodyByID(14));
                status.StatusType = Status.StatusTypes.Failure;
            }
            else if(account.Password == Cryptography.Encrypt(OldPassword, "tm-es@as"))
            {
                account.Password = Cryptography.Encrypt(NewPassword, "tm-es@as");
                IRepositoryStatus rStatus = ObjectFactory.GetInstance<IAccountRepository>().SaveAccount(account);

                status.StatusType = rStatus.StatusType;
                status.AddMessages(rStatus.GetMessages());
            }
            else
            {
                status.AddMessage(HelpMessageService.GetHelpMessageBodyByID(17));
            }
            return status;
        }

        public IStatus Logout()
        {
            IStatus status = ObjectFactory.GetInstance<IStatus>();
            try
            {
                ObjectFactory.GetInstance<IWebCookie>().Username = "";
                ObjectFactory.GetInstance<IWebCookie>().UsernameKey = "";
                ObjectFactory.GetInstance<IWebContext>().Account = null;
                ObjectFactory.GetInstance<IWebContext>().LoggedIn = false;
                ObjectFactory.GetInstance<IWebCookie>().Username = "";
                status.StatusType = Status.StatusTypes.Success;
            }
            catch (Exception)
            {
                status.StatusType = Status.StatusTypes.Failure;
            }
            return status;
        }

        public IStatus Login(string Username, string Password, bool RememberMe)
        {
            IStatus status = ObjectFactory.GetInstance<IStatus>();
            IAccount account = ObjectFactory.GetInstance<IAccountRepository>().GetAccountByUsername(Username);
            if(account != null) //good username
            {
                if(account.Password == Cryptography.Encrypt(Password, "tm-es@as")) //good password
                {
                    ObjectFactory.GetInstance<IWebContext>().Account = account;
                    IAccount check = ObjectFactory.GetInstance<IWebContext>().Account;
                    ObjectFactory.GetInstance<IWebCookie>().Username = account.Username;

                    if(RememberMe)
                    {
                        ObjectFactory.GetInstance<IWebCookie>().UsernameKey = account.Username;
                    }
                }
                else
                {
                    status.StatusType = Status.StatusTypes.Failure;
                    status.AddMessage(HelpMessageService.GetHelpMessageBodyByID(19));
                }
            }
            else
            {
                status.StatusType = Status.StatusTypes.Failure;
                status.AddMessage(HelpMessageService.GetHelpMessageBodyByID(18));
            }

            return status;
        }

        public IStatus AutoLogin(string username)
        {
            IAccount account = ObjectFactory.GetInstance<IAccountRepository>().GetAccountByUsername(username);
            IStatus status = ObjectFactory.GetInstance<IStatus>();
            if(account != null)
            {
                IStatus result = Login(account.Username, Cryptography.Decrypt(account.Password, "tm-es@as"), true);
                status.SetObject(account);
                status.StatusType = result.StatusType;
                status.AddMessages(result.GetMessages());
            }
            else
            {
                status.StatusType = Status.StatusTypes.Failure;
            }
            return status;
        }

        public IStatus Register(IAccount account)
        {
            IStatus s = ObjectFactory.GetInstance<IStatus>();
            account.Password = Cryptography.Encrypt(account.Password, "tm-es@as");
            if (ObjectFactory.GetInstance<IAccountRepository>().EmailExists(account.Email))
            {
                s.AddMessage(HelpMessageService.GetHelpMessageBodyByID(20));
                s.StatusType = Status.StatusTypes.Failure;
            }
            if (ObjectFactory.GetInstance<IAccountRepository>().UsernameExists(account.Username))
            {
                s.AddMessage(HelpMessageService.GetHelpMessageBodyByID(21));
                s.StatusType = Status.StatusTypes.Failure;
            }
            if (s.StatusType != Status.StatusTypes.Failure)
            {
                IRepositoryStatus rs = ObjectFactory.GetInstance<IAccountRepository>().SaveAccount(account);
                s.AddMessages(rs.GetMessages());
                s.StatusType = rs.StatusType;
            }
            return s;
        }
    }
}